using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using API;
using Grpc.Core;
using mb;
using Mb;
using Xam.Forms.VideoPlayer;
using Xamarin.Forms;

namespace libhot
{
	class StatusToBoolConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			// App.logger.Log("convert:{0},{1}", value, targetType.ToString());
			return (VideoStatus)value == VideoStatus.Playing ? true : false;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			// return ((bool)value) ? VideoStatus.Playing : VideoStatus.Paused;
			// App.logger.Log("convertBack:{0},{1}", value, targetType.ToString());
			return VideoStatus.Playing;
		}
	}

	public class VideoPage : ContentPage
	{
		View videoContainer;

		public VideoPage()
		{
			this.EnableNavBar(true);
			this.RegisterReload();
			App.logger.Log("VideoPage");
		}
		~VideoPage()
		{
			App.logger.Log("~VideoPage");
		}


		protected void DoLoad()
		{
			var global = CatLib.App.Make<IGlobal>();
			this.videoContainer = this.FindByName("vc") as View;
		}

		protected override void OnSizeAllocated(double width, double height)
		{
			base.OnSizeAllocated(width, height); //must be called
			var global = CatLib.App.Make<IGlobal>();
			App.logger.Log("size change:{0},{1}", width, height);
			if (width < height)
			{
				this.EnableNavBar(true);
				global.SetFullScreen(false);
				AbsoluteLayout.SetLayoutBounds(this.videoContainer, new Rectangle(0, 0, 1, 0.5));
			}
			else
			{
				this.EnableNavBar(false);
				global.SetFullScreen(true);
				AbsoluteLayout.SetLayoutBounds(this.videoContainer, new Rectangle(0, 0, 1, 1));
			}
		}

		void RegisterReload()
		{
			var hrm = CatLib.App.Make<HotReloadManager>();
			var unregister = hrm.RegisterReloadWeak(this, "video", (self, resName) =>
			{
				var me = self as VideoPage;
				me.Content = null;
				me.LoadXamlFromRes(resName);
				me.DoLoad();
			});
		}
		protected override void OnAppearing()
		{
			var global = CatLib.App.Make<IGlobal>();
			global.LockDeviceOrientation(DeviceOrientation.UP | DeviceOrientation.LEFT | DeviceOrientation.RIGHT);
		}


		protected override void OnDisappearing()
		{
			var global = CatLib.App.Make<IGlobal>();
			global.LockDeviceOrientation(DeviceOrientation.UP);

			var player = this.FindByName<Xam.Forms.VideoPlayer.VideoPlayer>("videoPlayer");
		}

		protected override bool OnBackButtonPressed()
		{
			var global = CatLib.App.Make<IGlobal>();
			var vcs = this.FindByName<VideoControlView>("vcs");
			if (vcs.IsLockFS)
			{
				vcs.OnClickFS();
				return true;
			}
			return false;
		}


		void OnClickVC(object sender, EventArgs e)
		{
			var view = sender as Layout<View>;
			App.logger.Log("click vc!,{0}", view.StyleId);
			MoveVideoPlayerTo(view);
		}

		void MoveVideoPlayerTo(Layout<View> vc)
		{
			var vp = this.FindByName<Xam.Forms.VideoPlayer.VideoPlayer>("videoPlayer");
			var vcs = this.FindByName<VideoControlView>("vcs");
			var keep = this.FindByName<Syncfusion.XForms.Buttons.SfCheckBox>("KeepPlaying").IsChecked;
			App.logger.Log("vp:{0}, keep:{1}", vp.Source?.RawData, keep);
			if (vp.Parent == vc) return;
			App.logger.Log("===0");

			if (keep == true) vp.KeepPlaying = true;

			((AbsoluteLayout)vp.Parent).Children.Remove(vp);
			vc.Children.Add(vp);

			((AbsoluteLayout)vcs.Parent).Children.Remove(vcs);
			vc.Children.Add(vcs);

			vp.KeepPlaying = false;
		}

	}
}